package com.lms.logistics_management_system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lms.logistics_management_system.controller.dto.DepartmentItemDto;
import com.lms.logistics_management_system.controller.dto.HistoryDiagnostic;
import com.lms.logistics_management_system.entity.Diagnostic;
import com.lms.logistics_management_system.entity.Doctor;
import com.lms.logistics_management_system.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
// @Mapper 配置放在配置类config最好不放在启动类
@Mapper
public interface DoctorMapper extends BaseMapper<Doctor> {

    boolean updateByDoctorId(Doctor doctor);

    @Delete("delete from doctor where doctor_id=#{doctorId}")
    boolean removeByDoctorId(Integer doctorId);

    boolean removeByDoctorIds(List<Integer> doctorIds);

    @Select("select * from doctor where doctor_id=#{doctorId}")
    Doctor getByDoctorId(Integer doctorId);

    @Select("select department_name from department where department.department_id=#{departmentId}")
    String getDepartmentName(Integer departmentId);

    @Select("SELECT i.item_id,mk.measure_kind_id,i.item_name, mk.measure_kind_name, mk.measure_kind_units\n" +
            "FROM reservation_item ri\n" +
            "JOIN reservation r ON ri.reservation_id = r.reservation_id\n" +
            "JOIN item i ON ri.item_id = i.item_id\n" +
            "JOIN department d ON i.department_id = d.department_id\n" +
            "JOIN item_measure_kind imk ON i.item_id = imk.\titem_id\n" +
            "JOIN measure_kind mk ON imk.measure_kind_id = mk.measure_kind_id\n" +
            "JOIN doctor doc ON doc.department_id=i.department_id\n" +
            "WHERE r.reservation_id = #{reservationId} AND doctor_id=#{doctorId};")
    List<DepartmentItemDto>  getDepartmentItem(Integer reservationId, Integer doctorId);

    @Select("select department_id from doctor where doctor_id=#{doctorId}")
    Integer getDepartment(Integer doctorId);

    @Select("SELECT DISTINCT r.reservation_id,MIN(ri.reservation_item_result_comeout) as reservation_item_result_comeout,r.reservation_name,MAX(dia.diagnostic_create_time) as diagnostic_create_time \n" +
            "FROM diagnostic dia\n" +
            "JOIN reservation r ON dia.reservation_id=r.reservation_id \n" +
            "JOIN reservation_item ri ON r.reservation_id=ri.reservation_id\n" +
            "WHERE doctor_id=#{doctorId} AND dia.item_id=ri.item_id\n" +
            "GROUP BY reservation_id\n" +
            "ORDER BY diagnostic_create_time DESC;")
    List<HistoryDiagnostic> getHistoryDiagnostic(Integer doctorId);

    @Select("SELECT d.item_id,i.item_name,d.diagnostic_result,m.measure_kind_id,mk.measure_kind_name,m.measure_result,mk.measure_kind_units\n" +
            "FROM diagnostic d\n" +
            "JOIN measure m ON d.diagnostic_id=m.diagnostic_id\n" +
            "JOIN measure_kind mk ON m.measure_kind_id=mk.measure_kind_id \n" +
            "JOIN item i ON d.item_id=i.item_id\n" +
            "WHERE d.reservation_id=#{reservationId} AND d.doctor_id=#{doctorId};")
    List<DepartmentItemDto> getDiagnosticInfo(Integer reservationId, Integer doctorId);


    @Select("select diagnostic_id,reservation_id,item_id,doctor_id,diagnostic_result,diagnostic_create_time from diagnostic " +
            "where reservation_id=#{reservationId} and doctor_id=#{doctorId}")
    List<Diagnostic> diagnosticIsExist(Integer reservationId, Integer doctorId);


    @Select("SELECT d.item_id,i.item_name,d.diagnostic_result,m.measure_kind_id,mk.measure_kind_name,m.measure_result,mk.measure_kind_units\n" +
            "FROM diagnostic d\n" +
            "JOIN measure m ON d.diagnostic_id=m.diagnostic_id\n" +
            "JOIN measure_kind mk ON m.measure_kind_id=mk.measure_kind_id \n" +
            "JOIN item i ON d.item_id=i.item_id\n" +
            "WHERE d.reservation_id=#{reservationId} ;")
    List<DepartmentItemDto> getDiagnosticByReservationId(Integer reservationId);
}
